// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.web.browser;

import org.dengues.ui.i18n.Messages;
import org.dengues.ui.web.browser.dom.HTMLDocument;
import org.dengues.ui.web.browser.dom.Node;
import org.dengues.ui.web.browser.dom.NodeList;
import org.dengues.ui.web.browser.event.DenguesProgressEvent;
import org.dengues.ui.web.browser.event.DenguesProgressListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author jliu
 */
public class BrowserTest {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        showDenguesBrowser(shell);
        // showSwtBrowser(shell);

        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private static void showSwtBrowser(Shell shell) {
        Browser browser = new Browser(shell, SWT.NONE);
        browser.setUrl("www.baidu.com"); //$NON-NLS-1$
    }

    /**
     * @param shell
     */
    private static void showDenguesBrowser(Shell shell) {
        final DenguesBrowser browser = new DenguesBrowser(shell);
        browser.navigate("www.baidu.com"); //$NON-NLS-1$
        browser.addProgressListener(new DenguesProgressListener() {

            public void changed(DenguesProgressEvent event) {

            }

            public void completed(DenguesProgressEvent event) {
                HTMLDocument document = browser.getDocument();
                NodeList childNodes = document.getAll();
                System.out.println(document.getNodeName());
                System.out.println(childNodes);
                int length = childNodes.getLength();
                System.out.println(length);
                for (int i = 0; i < length; i++) {
                    Node item = childNodes.item(i);
                    System.out.println(i + " Item " + item.getNodeName()); //$NON-NLS-1$
                }
            }
        });
    }
}
